﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LinqToEntityDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Bind(GetData());
            BindDdl();
        }
    }

    //分页功能的实现
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind(GetData());
    }

    //模糊查询实现
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        string searchKey = TbKeyword.Text.Trim();
        Bind(GetData(searchKey));
    }

    //编辑功能实现
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        //绑定数据
        Bind(GetData());
    }
    //编辑功能之更新功能的实现
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow selectRow = GridView1.Rows[e.RowIndex];
        int id = Convert.ToInt32(selectRow.Cells[1].Text);
        string title = (selectRow.Cells[2].Controls[0] as TextBox).Text;
        string content = (selectRow.Cells[3].Controls[0] as TextBox).Text;
        string author = (selectRow.Cells[5].Controls[0] as TextBox).Text;
        int clickNumber = Convert.ToInt32((selectRow.Cells[6].Controls[0] as TextBox).Text);
        UpdateData(id, title, content, author, clickNumber);
        //更新完成后，把当前页作为默认页
        GridView1.EditIndex = -1;
        //绑定数据
        Bind(GetData());
    }
    //编辑功能之取消功能的实现
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //GridView1.EditIndex默认为-1
        GridView1.EditIndex = -1;
        //绑定数据
        Bind(GetData());
    }

    //新增功能的实现
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        tb_info data = new tb_info();
        data.title = TbTitle.Text.Trim();
        data.content = TbContent.Text.Trim();
        data.author = TbAuthor.Text.Trim();
        data.publish_time = DateTime.Now;
        data.click_number = 0;
        data.category_mark = DdlCategory.SelectedValue;
        insertData(data);
        Bind(GetData());
    }

    //删除事件的实现
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var deleteId = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        DeleteData(deleteId);
        Bind(GetData());
    }


    private void Bind(object data)
    {
       // var results = GetData();
        GridView1.DataSource = data;
        GridView1.DataBind();
    }

    private void BindDdl() 
    {
        DdlCategory.DataValueField = "mark";
        DdlCategory.DataValueField = "name";
        DdlCategory.DataSource = new MysqlTestEntities().tb_category.ToList();
        DdlCategory.DataBind();
    }

    private static List<tb_info> GetData(string key = null)
    {
        var db = new MysqlTestEntities();
        if (key == null)
        {
            // var results = db.tb_info.ToList();
            var results = from info in db.tb_info
                          select info;
            return results.ToList();
        }
        else 
        {
            var results = from info in db.tb_info
                          where info.title.Contains(key)
                          select info;
            Debug.WriteLine(results);
            return results.ToList();
        }
        
    }

    private void UpdateData(int id, string title, string content, string author, int clickNumber)
    {
        //初始化数据库，即连接数据库,打开“Test”数据库
        var db = new MysqlTestEntities();
        //Single():返回序列唯一的元素，如果不是唯一，则引发异常
        var result = db.tb_info.Single(res => res.id == id);
        result.content = content;
        result.author = author;
        result.click_number = clickNumber;
        db.SaveChanges();
    }

    private void insertData(tb_info data)
    {
        var db = new MysqlTestEntities();
        db.tb_info.Add(data);
        db.SaveChanges();
    }

    private void DeleteData(int deleteId)
    {
        var db = new MysqlTestEntities();
        var data = db.tb_info.Single(res => res.id == deleteId);
        db.tb_info.Remove(data);
        db.SaveChanges();
    }


}